package com.laviedesfons.mavenjhmtascplugin;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import org.apache.html.dom.HTMLDOMImplementationImpl;
import org.apache.html.dom.HTMLDocumentImpl;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.html.HTMLElement;

/**
 * // TODO METTRE UN TITLE HTML AVEC LES INFOS DU PROJET
 *
 * chip this with swf packaging, and expose args to configuration
 *
 * @goal html
 * @author Romano
 */
public class HtmlMojo extends ParentMojo {

    private void displayHTMLFile(){
        if(verbose){
            FileReader fr = null;
            try {
                // further dev: verbose mode
                File outputFile = new File(outputDir + "/" + finalName + ".html");
                fr = new FileReader(outputFile);
                BufferedReader br = new BufferedReader(fr);
                String line;
                while ((line = br.readLine()) != null) {
                    getLog().info(line);
                }
            } catch (IOException ex) {
                getLog().error(ex);
            } finally {
                try {
                    fr.close();
                } catch (IOException ex) {
                    getLog().error(ex);
                }
            }
        }
    }

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
            try {
                String width = Integer.toString(getHeaderWidth());
                String height = Integer.toString(getHeaderHeight());
                
                HTMLDOMImplementationImpl domImpl = (HTMLDOMImplementationImpl) HTMLDOMImplementationImpl.getHTMLDOMImplementation();
                DocumentType docType = domImpl.createDocumentType("HTML", "-//W3C//DTD XHTML 1.0 Frameset//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd");
                HTMLDocumentImpl doc = (HTMLDocumentImpl) domImpl.createHTMLDocument("hello");
                doc.appendChild(docType);

                Element docRoot = doc.getDocumentElement();
                docRoot.setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
                docRoot.setAttribute("xml:lang", "fr");
                docRoot.setAttribute("lang", "fr");
                HTMLElement bodyElt = (HTMLElement) doc.createElement("body");
                HTMLElement swfObject = (HTMLElement) doc.createElement("object");
                swfObject.setAttribute("classid", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000");
                swfObject.setAttribute("codebase", "http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0");
                swfObject.setAttribute("width", width);
                swfObject.setAttribute("height", height);
                swfObject.setAttribute("id", id);
                swfObject.setAttribute("align", align);

                HTMLElement allowScriptAccessParam = (HTMLElement) doc.createElement("param");
                allowScriptAccessParam.setAttribute("name", "allowScriptAccess");
                allowScriptAccessParam.setAttribute("value", allowScriptAccess);
                swfObject.appendChild(allowScriptAccessParam);

                HTMLElement movieParam = (HTMLElement) doc.createElement("param");
                movieParam.setAttribute("name", "movie");
                movieParam.setAttribute("value", outputDir+"/"+finalName+".swf");
                swfObject.appendChild(movieParam);

                HTMLElement qualityParam = (HTMLElement) doc.createElement("param");
                qualityParam.setAttribute("name", "quality");
                qualityParam.setAttribute("value", quality);
                swfObject.appendChild(qualityParam);

                HTMLElement bgcolorParam = (HTMLElement) doc.createElement("param");
                bgcolorParam.setAttribute("name", "bgcolor");
                bgcolorParam.setAttribute("value", bgcolor);
                swfObject.appendChild(bgcolorParam);

                HTMLElement embedElt = (HTMLElement) doc.createElement("embed");
                embedElt.setAttribute("src", outputDir+"/"+finalName+".swf");
                embedElt.setAttribute("quality", quality);
                embedElt.setAttribute("bgcolor", bgcolor);
                embedElt.setAttribute("width", width);
                embedElt.setAttribute("height", height);
                embedElt.setAttribute("name", name);
                embedElt.setAttribute("align", align);
                embedElt.setAttribute("allowScriptAccess", allowScriptAccess);
                embedElt.setAttribute("type", "application/x-shockwave-flash");
                embedElt.setAttribute("pluginspage", "http://www.macromedia.com/go/getflashplayer");
                swfObject.appendChild(embedElt);

                bodyElt.appendChild(swfObject);
                doc.setBody(bodyElt);
                OutputFormat of = new OutputFormat(doc);
                of.setIndenting(true);
                of.setOmitXMLDeclaration(true);
                XMLSerializer serializer = new XMLSerializer();
                serializer.setOutputFormat(of);
                File outputFile = new File(outputDir+"/"+finalName+".html");
                serializer.setOutputByteStream(new FileOutputStream(outputFile));
                serializer.serialize(doc);

    //                displayHTMLFile();
            } catch (IOException ex) {
                getLog().error(ex);
            }
    }
}